﻿#region Copyright(c) 2006 ZO, All right reserved.
// -----------------------------------------------------------------------------
// Copyright(c) 2006 ZO, All right reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//   1.  Redistribution of source code must retain the above copyright notice,
//       this list of conditions and the following disclaimer.
//   2.  Redistribution in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//   3.  The name of the author may not be used to endorse or promote products
//       derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// -----------------------------------------------------------------------------
#endregion


#region Using directives

using System.Text;

#endregion

namespace ZO.SmartCore.Standards.Unicode.Normalization
{
    /// <summary>
    /// Hangul canonical decomposition and composition
    /// </summary>
    /// <remarks>
    /// Entire hangul code copied from: http://www.unicode.org/unicode/reports/tr15/
    /// </remarks>
    internal static class Hangul
    {

        #region Constructors

        #endregion

        #region Destructor

        #endregion

        #region Fields
        private const int SBase = 0xAC00;
        private const int LBase = 0x1100;
        private const int VBase = 0x1161;
        private const int TBase = 0x11A7;
        private const int LCount = 19;
        private const int VCount = 21;
        private const int TCount = 28;
        private const int NCount = VCount * TCount;
        private const int SCount = LCount * NCount;

        #endregion

        #region Events

        #endregion

        #region Operators

        #endregion

        #region Properties

        #endregion

        #region Methods
        /// <summary>
        /// Composes two hangul characters.
        /// </summary>
        /// <param name="a">First character</param>
        /// <param name="b">Second character</param>
        /// <returns>the composed character or -1 if the two characters cannot be composed.</returns>
        public static int Compose(char a, char b)
        {
            // 1. check to see if two current characters are L and V
            int LIndex = a - LBase;
            if (0 <= LIndex && LIndex < LCount)
            {
                int VIndex = b - VBase;
                if (0 <= VIndex && VIndex < VCount)
                {
                    // make syllable of form LV
                    return SBase + (LIndex * VCount + VIndex) * TCount;
                }
            }

            // 2. check to see if two current characters are LV and T
            int SIndex = a - SBase;
            if (0 <= SIndex && SIndex < SCount && (SIndex % TCount) == 0)
            {
                int TIndex = b - TBase;
                if (0 <= TIndex && TIndex <= TCount)
                {
                    // make syllable of form LVT
                    return a + TIndex;
                }
            }
            return -1;
        }

        /// <summary>
        /// Decomposes a hangul character.
        /// </summary>
        /// <param name="c">A character to decompose.</param>
        /// <returns>A string containing the hangul decomposition of the input character. If no hangul decomposition can be found, a string
        /// containing the character itself is returned.</returns>
        public static string Decompose(char c)
        {
            int SIndex = c - SBase;
            if (SIndex < 0 || SIndex >= SCount)
            {
                return c.ToString();
            }

            StringBuilder result = new StringBuilder();

            int L = LBase + SIndex / NCount;

            int V = VBase + (SIndex % NCount) / TCount;
            int T = TBase + SIndex % TCount;

            result.Append((char)L);
            result.Append((char)V);
            if (T != TBase) result.Append((char)T);
            return result.ToString();
        }

        #endregion
    }
}
